import java.util.Arrays;

public class SolutionInterview8_10 {

    int[] dx=new int[]{0,1,0,-1};
    int[] dy=new int[]{1,0,-1,0};

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        dfs(image,sr,sc,newColor);
        return image;
    }

    private void dfs(int[][] image, int sr, int sc, int newColor) {
        if (image[sr][sc]==newColor){
            return;
        }
        int origin=image[sr][sc];
        image[sr][sc]=newColor;
        for (int i = 0; i < 4; i++) {
            int x=sr+dx[i],y=sc+dy[i];
            if (x>=0 && x<image.length && y>=0 && y<image[0].length && origin==image[x][y]){
                dfs(image,x,y,newColor);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(new SolutionInterview8_10().floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2)));
    }
}
